package com.lhr.common.search;

import java.io.IOException;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.LockObtainFailedException;


public class LuceneIndexer {

	private LuceneSettings	settings;

	public LuceneIndexer(LuceneSettings settings) {
		this.settings = settings;
	}

	public IndexWriter createIndexWriter() throws CorruptIndexException, LockObtainFailedException, IOException {
		IndexWriterConfig indexWriterConfig = new IndexWriterConfig(settings.getVersion(), settings.getAnalyzer());
		if (settings.getOpenMode() != null)
			indexWriterConfig.setOpenMode(settings.getOpenMode());
		IndexWriter writer = new IndexWriter(settings.getDirectory(), indexWriterConfig);
		return writer;
	}

	public Document createDocument(Field... fields) {
		Document document = new Document();
		for (Field field : fields) {
			document.add(field);
		}
		return document;
	}

	public Field createField(String name, String text) {
		return createField(name, text, Field.Store.YES, Field.Index.ANALYZED);
	}

	public Field createField(String name, String text, Store fieldStore, Index fieldIndex) {
		return new Field(name, text, fieldStore, fieldIndex);
	}
}
